import requests
from bs4 import BeautifulSoup
import last.store as store


def get_data(url, type):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64)'
    }
    r = requests.get(url=url, headers=headers)
    if r.status_code == 200:
        r.encoding = 'utf-8'
        page_text = r.text
        soup = BeautifulSoup(page_text, 'html.parser')
    data = {}
    try:
        data['type'] = type
        data['gdm'] = soup.find('h2', class_="f-ff2 f-brk").string
        data['gqs'] = int(soup.find('span', id='playlist-track-count').get_text())
        data['pls'] = int(soup.find('span', id='cnt_comment_count').get_text())
        data['bfl'] = int(soup.find('strong', class_='s-fc6').get_text())
        data['scs'] = int(soup.find('a', class_='u-btni u-btni-fav').get('data-count'))
    except:
        pass
    store.saveinMySQL(data)
    return data
